package com.jyniubi.base.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jyzhang
 * 统一定义返回结果
 */

@Data
public class R {
    private Integer code;
    private String msg;
    private Map<String,Object> data = new HashMap<>();
    // 返回成功
    public static R ok(){
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMsg(ResponseEnum.SUCCESS.getMessage());
        return r;
    }
    // 返回失败
    public static R error(){
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMsg(ResponseEnum.ERROR.getMessage());
        return r;
    }
    // 通过枚举对象给R对象设置code和message属性值
    public static R setResult(ResponseEnum responseEnum){
        R r = new R();
        r.setCode(responseEnum.getCode());
        r.setMsg(responseEnum.getMessage());
        return r;
    }
    public R code(Integer code){
        this.code = code;
        return this;
    }
    public R msg(String msg){
        this.msg = msg;
        return this;

    }
    //向r对象的data中存入一对新的k-v数据
    public R data(String key,Object val){
        this.data.put(key,val);
        return this;
    }
    //使用一个新的map引用赋值给r对象的data属性
    public R data(Map<String,Object> data){
        this.data = data;
        return this;
    }
}
